//
//  SFLoginController.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/7/13.
//

import UIKit

class SFLoginController: SFBaseController {

    var mobileTf: SFTextField?
    var passwordTf: SFTextField?
    var loginBtn: UIButton?
    var complete: ((_ v: Bool)->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        hideNavigationBar = true
    }
    
    override func setup() {
        super.setup()
        
        let backBtn = UIButton.init(type: .custom)
        backBtn.setImage(UIImage(named: "icon_base_back_arrow"), for: .normal)
        view.addSubview(backBtn)
        backBtn.addTarget(self, action: #selector(popBackAction), for: .touchUpInside)
        backBtn.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(Adapter.Screen.statusBarHeight)
            make.width.height.equalTo(44)
        }
        
        let title = UILabel.init()
        title.text = "登录更精彩"
        title.font = RegularFont(v: 28)
        title.textColor = kBlackColor
        view.addSubview(title)
        title.snp.makeConstraints { make in
            make.top.equalTo(backBtn.snp.bottom).offset(40)
            make.left.equalTo(28)
        }
        
        let logo = UIImageView.init(image: UIImage(named: "icon_login_logo"))
        view.addSubview(logo)
        logo.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.top.equalToSuperview().offset(Adapter.Screen.statusBarHeight)
            make.size.equalTo(CGSize.init(width: 147, height: 130))
        }
        
        let mobileTf = SFTextField.init(frame: .zero)
        self.mobileTf = mobileTf
        mobileTf.addTarget(self, action: #selector(textFieldValueChanged(tf:)), for: .editingChanged)
        mobileTf.edgeInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        mobileTf.attributedPlaceholder = NSAttributedString.init(string: "请输入手机号", attributes: [.foregroundColor: kBlackColor_30,.font: RegularFont(v: 13)])
        mobileTf.cornerRadius = 3
        mobileTf.keyboardType = .numberPad
        mobileTf.backgroundColor = kBlackColor_05
        view.addSubview(mobileTf)
        mobileTf.snp.makeConstraints { make in
            make.left.equalTo(28)
            make.top.equalTo(title.snp.bottom).offset(65)
            make.right.equalTo(-28)
            make.height.equalTo(44)
        }
        
        let passwordTf = SFTextField.init(frame: .zero)
        self.passwordTf = passwordTf
        passwordTf.addTarget(self, action: #selector(textFieldValueChanged(tf:)), for: .editingChanged)
        passwordTf.edgeInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        passwordTf.attributedPlaceholder = NSAttributedString.init(string: "请输入密码", attributes: [.foregroundColor: kBlackColor_30,.font: RegularFont(v: 13)])
        passwordTf.cornerRadius = 3
        passwordTf.backgroundColor = kBlackColor_05
        passwordTf.isSecureTextEntry = true
        view.addSubview(passwordTf)
        passwordTf.snp.makeConstraints { make in
            make.left.right.height.equalTo(mobileTf)
            make.top.equalTo(mobileTf.snp.bottom).offset(12)
        }
        
        let loginBtn = UIButton.init(type: .custom)
        self.loginBtn = loginBtn
        loginBtn.titleLabel?.font = MediumFont(v: 18)
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.setTitleColor(kWhiteColor, for: .normal)
        loginBtn.backgroundColor = kBlackColor_20
        loginBtn.cornerRadius = 5
        loginBtn.addTarget(self, action: #selector(loginBtnAction), for: .touchUpInside)
        view.addSubview(loginBtn)
        loginBtn.snp.makeConstraints { make in
            make.left.right.equalTo(passwordTf)
            make.top.equalTo(passwordTf.snp.bottom).offset(80)
            make.height.equalTo(44)
        }
        
    }
    
    @objc func popBackAction() {
        self.dismiss(animated: true) {
            self.complete?(false)
        }
    }
    
    @objc func loginBtnAction() {
        if mobileTf?.text?.count ?? 0 <= 0 {
            AppHelper.showInfo(info: "手机号码有误")
            return
        }
        if passwordTf?.text?.count ?? 0 < 0 {
            AppHelper.showInfo(info: "密码有误")
            return
        }
        
        let param = [
            "mobile" : mobileTf?.text ?? "",
            "password" : passwordTf?.text ?? "",
            "openid" : "",
        ]

        SFNetwork.post(url: kAPI_user_login, params: param) { response in
            switch response {
                
            case .success(let res):
                let v = UserInfo.deserialize(from: res.data)
                AppUser.saveUser(info: v);
                Login.setLogin(state: v != nil)
                self.dismiss(animated: true) {
                    self.complete?(true)
                }
                break
                
            case .fail(let error):
                AppHelper.showInfo(info: error.errMsg)
                break
            
            }
        }
    }
    
    @objc func textFieldValueChanged(tf: UITextField) {
        if tf == mobileTf {
            if tf.text?.count ?? 0 > 11 {
                tf.text = (tf.text as NSString?)!.substring(to: 11)
            }
        }else if tf == passwordTf {
            if tf.text?.count ?? 0 > 16 {
                tf.text = (tf.text as NSString?)!.substring(to: 16)
            }
        }
        loginBtn!.backgroundColor = (mobileTf?.text!.count ?? 0 > 0 && passwordTf?.text?.count ?? 0 > 0) ? UIColor(hex: 0xfc4041,alpha: 0.8) : kBlackColor_20
    }

    

}
